
package ai;

import java.awt.image.BufferedImage;

public class ScreenImgBinBorder
{
	public static final int	max	= -1;			//new Color(255, 255, 255).getRGB();
	public static final int	min	= -16777216;	//new Color(0, 0, 0).getRGB();
	
	public static float [] getHeightInfo(BufferedImage bi)
	{
		int h = bi.getHeight();//768
		int w = bi.getWidth();//1366
		float imgInfo_h[] = new float[h];
		for(int k = 0; k < h; k++)
		{
			int whitePoint = 0;
			for(int l = 0; l < w; l++)
			{
				if(bi.getRGB(l, k) == ScreenImgBinBorder.max)
				{
					whitePoint++;
				}
			}
			imgInfo_h[k] = whitePoint / (float) h;
		}
		return imgInfo_h;
	}
	
	public static float [] getWidthInfo(BufferedImage bi)
	{
		int h = bi.getHeight();//768
		int w = bi.getWidth();//1366
		float imgInfo_w[] = new float[w];
		for(int k = 0; k < w; k++)
		{
			int whitePoint = 0;
			for(int l = 0; l < h; l++)
			{
				if(bi.getRGB(k, l) == ScreenImgBinBorder.max)
				{
					whitePoint++;
				}
			}
			imgInfo_w[k] = whitePoint / (float) h;
		}
		return imgInfo_w;
	}
	
	public static boolean [] getNeedLines(float [] imgInfo)
	{
		boolean [] result = new boolean[imgInfo.length];
		for(int i = 0; i < imgInfo.length; i++)
		{
			if(imgInfo[i] > 0.11)
			{
				result[i] = true;
			}
		}
		return result;
	}
	
	public static BufferedImage toLineImg(boolean [] usedWidthLine, boolean [] usedHeightLine, int w, int h)
	{
		BufferedImage nbi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
		for(int i = 0; i < w; i++)
		{
			if(usedWidthLine[i] == true)
			{
				for(int j = 0; j < h; j++)
				{
					nbi.setRGB(i, j, ScreenImgBinBorder.max);
				}
			}
		}
		for(int i = 0; i < h; i++)
		{
			if(usedHeightLine[i] == true)
			{
				for(int j = 0; j < w; j++)
				{
					nbi.setRGB(j, i, ScreenImgBinBorder.max);
				}
			}
		}
		return nbi;
	}
	
	public static int [] getUseLines(boolean [] needLines)
	{
		int num = 0;
		for(int i = 0; i < needLines.length; i++)
		{
			if(needLines[i] == true)
			{
				num++;
			}
		}
		int [] lineNums = new int[num];
		num = 0;
		for(int i = 0; i < needLines.length; i++)
		{
			if(needLines[i] == true)
			{
				lineNums[num] = i;
				num++;
			}
		}
		return lineNums;
	}
	
	public static int [] getEqualDistacePoint(int [] ary)
	{
		int [] result = new int[ary.length];
		for(int i = 0; i < (ary.length - 1); i++)
		{
			int subValue = ary[i + 1] - ary[i];
			if((subValue > 55) && (subValue < 65))
			{
				result[i] = ary[i];
				result[i + 1] = ary[i + 1];
			}
		}
		int num = 0;
		for(int i = 0; i < result.length; i++)
		{
			if(result[i] != 0)
			{
				num++;
			}
		}
		int [] res = new int[num];
		if(num != 7)
		{
			System.out.println("识别边界矩阵发生错误");
			return null;
		}
		num = 0;
		for(int i = 0; i < result.length; i++)
		{
			if(result[i] != 0)
			{
				res[num++] = result[i];
			}
		}
		return res;
	}
	
	public static BufferedImage toEqualDistanceLineImg(int [] equalWidthPos, int [] equalHeightPos, int width, int height)
	{
		BufferedImage nbi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
		for(int i = 0; i < equalWidthPos.length; i++)
		{
			if(equalWidthPos[i] < width)
			{
				for(int j = 0; j < height; j++)
				{
					nbi.setRGB(equalWidthPos[i], j, ScreenImgBinBorder.max);
				}
			}
		}
		for(int i = 0; i < equalHeightPos.length; i++)
		{
			if(equalHeightPos[i] < height)
			{
				for(int j = 0; j < width; j++)
				{
					nbi.setRGB(j, equalHeightPos[i], ScreenImgBinBorder.max);
				}
			}
		}
		return nbi;
	}
}
